package com.sosee.cgfw.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;

import com.sosee.cgfw.interfac.PassParametersInterface;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.wujay.fund.GestureVerifyActivity;

public class ExampleService extends Service implements PassParametersInterface {
	private static final Map<String, Object> parametersMap = new HashMap<String, Object>();
	private boolean isTrue=true;
	private String mGesture;
	private SharedPreferences sp;
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		sp=getSharedPreferences(Constans.GESTURENAME,MODE_PRIVATE);
		startCheckActivityThread();
		
	}

	
	Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {//APP在在屏幕不可见了
				isTrue=false;
                // 处理
            	parametersMap.put("OUTAPP", "OUTAPP");
            }else{
				if(!isTrue){
					mGesture=sp.getString(Constans.GESTURE,"");
					if(mGesture!=null&&mGesture.length()>3){
						Intent intent=new Intent(ExampleService.this, GestureVerifyActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					}
					isTrue=true;
				}else{

				}

			}
        }
    };


	public int onStartCommand(Intent intent, int flags, int startId) {
		LogUtils.log_E("进程被杀死");
		return START_STICKY;
	}
    
    
	/**
	 * 开启检查Activity活动的子线程
	 */
	private void startCheckActivityThread() {
		new Thread() {
			public void run() {
				
				Process.setThreadPriority(-20);
				
				while(true){
					try {
						if(!isAppOnForeground()){
							handler.sendEmptyMessage(1);
						}else{
							handler.sendEmptyMessage(2);
						}
						Thread.sleep(1000);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}.start();

	}

	/**
	 *  在进程中去寻找当前APP的信息，判断是否在前台运行
	 * @return
	 */
	private boolean isAppOnForeground() {
		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}
		return false;
	}

	@Override
	public Map<String, Object> getParameters() {
		LogUtils.log_E("Service里的Map值为：" + parametersMap.get("OUTAPP"));
		return parametersMap;
	}

	public void setParametersMapNull() {
		this.parametersMap.clear();;
	}
	
	@Override
	public void onDestroy() {
		onCreate();
		super.onDestroy();
	}
	
	

}
